<template>
	<view class="content">
		<u-form :model="form" ref="uForm" label-width="140">
			<u-form-item label="事项名称" prop="wpsName"><u-input v-model="form.wpsName" placeholder="事项名称" /></u-form-item>
			<u-form-item label="客户"><u-input v-model="form.custName" type="select" placeholder="请选择客户" @click="custShow = true"/></u-form-item>
			<u-form-item label="事项类别" prop="planBussTypeName"><u-input v-model="form.planBussTypeName" type="select" placeholder="事项类别"  @click="typeShow = true"/></u-form-item>
			<u-form-item label="起始时间" prop="beginDate">
				<u-input v-model="form.beginDate" placeholder="请选择起始时间" type="select" @click="beginShow = true" />
			</u-form-item>
			<u-form-item label="结束时间" prop="endDate">
				<u-input v-model="form.endDate" placeholder="请选择结束时间" type="select" @click="endShow = true" />
			</u-form-item>
			<u-form-item label="备注" prop="biz3"><u-input v-model="form.biz3" /></u-form-item>
			
			<view class="btn" @click="addSomthing">确定</view>
		</u-form>
		<u-picker mode="time" :params="params" v-model="beginShow" @confirm="beginChange"></u-picker>
		<u-picker mode="time" :params="params" v-model="endShow" @confirm="endChange"></u-picker>
		<u-select v-model="typeShow" :list="list" @confirm="typeConfirm"></u-select>
		<u-select v-model="custShow" :list="custList" @confirm="custConfirm"></u-select>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				beginShow: false,
				endShow: false,
				typeShow: false,
				custShow: false,
				list: [
					{ value: '1', label: '销售' },
					{ value: '2', label: '跟踪' },
					{ value: '3', label: '项目' },
					{ value: '4', label: '其他' },
				],
				custList: [],
				rules: {
					wpsName: [
						{ 
							required: true, 
							message: '请输入事项名称', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change','blur'],
						}
					],
					planBussTypeName: [
						{
							required: true, 
							message: '请选择事项类别', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change'],
						}
					],
					beginDate: [
						{
							required: true, 
							message: '请选择起始时间', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change'],
						}
					],
					endDate: [
						{
							required: true, 
							message: '请选择结束时间', 
							// 可以单个或者同时写两个触发验证方式 
							trigger: ['change'],
						}
					]
				},
				iconStyle: {
					'fontSize': '38rpx'
				},
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: true,
				},
				form: {
					biz3: '',
					beginDate: '',
					endDate: '',
					
					planBussType: "",
					planBussTypeName: "",
					wpsName: "",
					custName: "",
					custId: ""
				},
			};
		},
		methods: {
			getCustList(){
				this.$api.user.getCusListByEmpId().then((res) => {
					console.log("res",res);
					this.custList = res.map(item => {
						return {
							value: item.custManId,
							label: item.custManName
						}
					})
				})
			},
			addSomthing(){
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						this.$api.user.addCrmBasWps({
							biz1: this.form.wpsName,
							custId: this.form.custId,
							custName: this.form.custName,
							planBussType: this.form.planBussTypeName,
							planDayFrom: this.form.beginDate,
							planDayTo: this.form.endDate,
							deptEmpNo: uni.getStorageSync("THCRMUSERCODE"),
							biz3: this.form.biz3
						}).then((res) => {
							console.log("res",res);
							uni.showToast({
								title: '新建成功',
								duration: 2000,
								success: () => {
									uni.navigateBack();
								}
							});
						})
					} else {
						console.log('验证失败');
					}
				});
				
			},
			beginChange(e) {
				console.log(e);
				this.form.beginDate = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + e.second;
			},
			endChange(e) {
				this.form.endDate = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute + ':' + e.second;
			},
			typeConfirm(val){
				console.log(val);
				this.form.planBussType = val[0].value;
				this.form.planBussTypeName = val[0].label;
			},
			custConfirm(val){
				this.form.custId = val[0].value;
				this.form.custName = val[0].label;
			}
		},
		onLoad() {
			this.getCustList()
		},
		// 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		}
	}
</script>

<style lang="scss">
	.content{
		padding: 10rpx 30rpx;
		.btn{
			width: 100%;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			background-color: #007AFF;
			color: #FFFFFF;
			border-radius: 10rpx;
			margin-top: 20rpx;
		}
	}
</style>
